//
//  ICLcbButtonLayout.m
//  ICome
//
//  Created by Administrator on 2016/12/15.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICLcbButtonLayout.h"
#import "ICHeaderFile.h"


@interface ICLcbButtonLayout()
/** URL*/
@property (nonatomic ,strong)NSString * routingClick;
/** 路由名*/
@property (nonatomic ,strong)NSString * eventName;


/** 图标*/
@property (nonatomic ,strong)UIImageView * iconImageView;
/** 文字*/
@property (nonatomic ,strong)UILabel * namelabel;



@end

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincomplete-implementation"
@implementation ICLcbButtonLayout
#pragma clang diagnostic pop



+(instancetype)buttonWithType:(UIButtonType)buttonType withFrame:(CGRect)frame withEventWithName:(NSString *)eventNameStr withClick:(NSString*)routingStr
{
    ICLcbButtonLayout * button = [super buttonWithType:buttonType];
    button.frame = frame;
    button.routingClick = routingStr;
    button.eventName = eventNameStr;
    if(eventNameStr != nil)
    {
        [button addTarget:button action:@selector(Mybutton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return button;
}
//    id = 2;
//    imageUrl = "http://183.196.130.125:9001/interface/v0.98/newhand/images/iwork.1.2.png";
//    title = "e\U6808\U5546\U65c5";
//    url = "";
-(void)setDict:(NSDictionary *)dict
{
    _dict = dict;
    if(_dict != nil){
        [self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(10 * App_Frame_Width/375.0f);
            make.centerX.equalTo(self.mas_centerX);
            make.width.mas_equalTo(40*App_Frame_Width/375.0f);
            make.height.mas_equalTo(40*App_Frame_Width/375.0f);
        }];
        [self.namelabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconImageView.mas_bottom).offset(4 * App_Frame_Width/375.0f);
            make.centerX.equalTo(self.mas_centerX);
            make.width.mas_equalTo(self.width - 10);
            make.height.mas_equalTo(15*App_Frame_Width/375.0f);
        }];
        //self.iconImageView.clipsToBounds  = YES;
         NSString *url = [ICTools getFileOglImageURL:(_dict[@"photoId"])];//@"icon_application_bg"
        self.namelabel.text = _dict[@"appName"];//_dict[@"title"];
        NSURL *imgUrl = [NSURL URLWithString:url];//_dict[@"imageUrl"]
        [self.iconImageView sd_setImageWithURL:imgUrl placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_ework_service_default"]];
        //icon_ework_service_default  icon_application_bg
    }
    
}

-(void)Mybutton:(ICLcbButtonLayout *)btn
{
//    if(self.eventName!=nil){
//        if(self.routingClick.length > 0){
//            [self routerEventWithName:self.eventName userInfo:@{@"url":self.routingClick}];
//        }else{
//             [self routerEventWithName:self.eventName userInfo:@{@"url":@"IComerror:"}];
//        }
//    }
    
    NSString * url = nil;
    NSString * appType = [NSString stringWithFormat:@"%@",self.dict[@"appType"]];
    //        if (![ICTools stringEmpty:dict[@"appClassId"]]) {
    if ([appType isEqualToString:@"1"] ||[appType isEqualToString:@"4"]) {
        url = self.dict[@"lightAppURL"];
        if(url){
            [self routerEventWithName:self.eventName userInfo:@{@"url":url}];
        }else{
            [self routerEventWithName:self.eventName userInfo:@{@"url":@"IComerror:"}];
        }
        
        //[self pushCustomViewControllerURL:model.lightAppURL fromViewController:self animated:YES];
    } else if ([appType isEqualToString:@"2"] || [appType isEqualToString:@"5"] ) {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:self.dict[@"iosScheme"]]]) {
            //url = [ICJSBridgeTools appendUserInfoforJSURL:self.dict[@"iosScheme"]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[ICJSBridgeTools appendUserInfoforJSURL:self.dict[@"iosScheme"]]]];
        } else {
            //url = self.dict[@"iosURL"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.dict[@"iosURL"]]];
        }
    }else{
        [self routerEventWithName:self.eventName userInfo:@{@"url":@"IComerror:"}];
    }
}

-(UIImageView *)iconImageView
{
    if(!_iconImageView)
    {
        _iconImageView = [[UIImageView alloc] init];
        //_iconImageView.contentMode =  UIViewContentModeScaleAspectFill;
        [self addSubview:_iconImageView];
    }
    return _iconImageView;
}

-(UILabel *)namelabel
{
    if(!_namelabel)
    {
        _namelabel = [[UILabel alloc]init];
        _namelabel.textColor = ICRGB(0x333333);
        _namelabel.font = [UIFont systemFontOfSize:12];
        _namelabel.textAlignment = NSTextAlignmentCenter;
        _namelabel.numberOfLines = 1;
        [self addSubview:_namelabel];
    }
    return _namelabel;
}

-(void)setBorder:(ICLcbBorderMask)mask
{
    
}


@end
